/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Graphics;
import java.awt.Point;

/**
 *
 * @author Green
 */
public class Motion {

    public enum MType {

        Deal, Play, Lose
    };
    private GUICard card;
    private Point target;
    private boolean finished = false;
    private int speed = 12;
    private MType type;
    private IMotionFinish receiver;


    public GUICard getCard() {
        return card;
    }

    public MType getType() {
        return type;
    }

    public Motion(GUICard card, Point target, IMotionFinish receiver, MType type) {
        this.card = card;
        this.target = target;
        this.type = type;
        this.receiver = receiver;
    }

    public boolean isFinished() {
        return this.finished;
    }

    public void update() {
        if (this.finished) {
            return;
        }

        int dx = this.target.x - this.card.position.x;
        int dy = this.target.y - this.card.position.y;

        int steps;
        float xIncre, yIncre;
        if (Math.abs(dx) > Math.abs(dy)) {
            steps = Math.abs(dx);
        } else {
            steps = Math.abs(dy);
        }
        
        xIncre = (dx / (float) steps) * speed;
        yIncre = (dy / (float) steps) * speed;

        this.card.position.x += xIncre;
        this.card.position.y += yIncre;

        double l = Point.distance(this.card.position.x, this.card.position.y, this.target.x, this.target.y);
        if (l < speed) {
            this.card.position = (Point)this.target.clone();
            this.finished = true;
        }
    }

    public void draw(Graphics g) {
        if (this.finished) {
            return;
        }

        this.card.draw(g);
    }

    public void notifyReceiver() {
        this.receiver.end(this);
    }
}
